package cn.org.eshow.service.impl;

import cn.org.eshow.bean.query.BillQuery;
import cn.org.eshow.common.page.Page;
import cn.org.eshow.dao.BillDao;
import cn.org.eshow.model.Bill;
import cn.org.eshow.model.User;
import cn.org.eshow.service.BillManager;
import cn.org.eshow.util.OrderUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.jws.WebService;
import java.util.Date;
import java.util.List;

@WebService(serviceName = "BillService", endpointInterface = "cn.org.eshow.service.BillManager")
@Service
public class BillManagerImpl extends GenericManagerImpl<Bill, Integer> implements BillManager {

    @Autowired
    private BillDao billDao;

    public BillManagerImpl() {
    }

    @Autowired
    public BillManagerImpl(BillDao billDao) {
        super(billDao);
        this.billDao = billDao;
    }

    @Override
    public List<Bill> list(BillQuery query) {
        return billDao.list(query);
    }

    @Override
    public Page<Bill> search(BillQuery query) {
        return billDao.search(query);
    }


    @Override
    public Long count(BillQuery query) {
        return billDao.count(query);
    }

    @Override
    public List<Bill> list(String hql, Object[] params) {
        return billDao.list(hql, params);
    }

    @Override
    public Bill updateBill(Bill old, Bill bill) {
        old.setOrderNo(StringUtils.isNotEmpty(bill.getOrderNo())?bill.getOrderNo():old.getOrderNo());
        old.setOrderType(StringUtils.isNotEmpty(bill.getOrderType())?bill.getOrderType():old.getOrderType());
        old.setRemark(StringUtils.isNotEmpty(bill.getRemark())?bill.getRemark():old.getRemark());
        old.setOutlay(bill.getOutlay()!=null?bill.getOutlay():old.getOutlay());
        return billDao.save(old);
    }

    @Override
    public void calc(User user, String category, String type, Double receive, Double outlay, Double balance, String orderType, String orderNo, String remark, Integer state, String img) {
        Bill bill = new Bill();
        bill.setAddTime(new Date());
        bill.setNo(OrderUtil.getOrderNo());
        bill.setCategory(category);
        bill.setType(type);
        bill.setReceive(receive);
        bill.setOutlay(outlay);
        if (balance != null && !"".equals(balance)) {
            bill.setBalance(balance);
        }
        bill.setOrderType(orderType);
        bill.setOrderNo(orderNo);
        bill.setState(state);
        bill.setRemark(remark);
        bill.setImg(img);
        bill.setUser(user);
        billDao.save(bill);
    }

}